package com.athuahua.spzx.controller.system;


import com.athuahua.spzx.annotation.AutoLog;
import com.athuahua.spzx.entity.SysMenu;
import com.athuahua.spzx.deprecated_entity.system.SysMenuHasChildren;
import com.athuahua.spzx.service.SysMenuService;
import com.athuahua.spzx.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: huahua
 * @Date: 2024/7/24 0024
 * @Time: 9:56
 * @Description:
 */
@Tag(name = "用户菜单接口")
@RestController
@RequestMapping("/admin/system/sysMenu")
@Slf4j
public class SysMenuController {

    @Resource
    private SysMenuService sysMenuService;

    //findNodes
    @GetMapping("findNodes")
    @Cacheable(cacheNames = "sysMenuFindNodes")
    @Operation( summary = "查询菜单树" )
    public Result findNodes() {
        log.info("findNodes");
        List<SysMenuHasChildren> findNodes = sysMenuService.findNodes();

        return Result.success(findNodes);
    }

    //保存save
    @PostMapping("save")
    @CacheEvict(cacheNames = "sysMenuFindNodes",allEntries=true)
    @Operation( summary = "保存菜单" )
    @AutoLog(title = "菜单管理模块新增", businessType = 1) //记录日志使用
    @Transactional //后续代码都是递归调用，所以放在controller层中
    public Result save(@RequestBody SysMenu sysMenu) {
        log.info("save {}",sysMenu);
        sysMenuService.save(sysMenu);
        //新添加了子菜单，把父菜单isHalf修改为1
        if(sysMenu.getParentId()!= 0)
            sysMenuService.updateParent(sysMenu);


        return Result.success();
    }

    //update
    @PutMapping("update")
    @CacheEvict(cacheNames = "sysMenuFindNodes",allEntries=true)
    @Operation( summary = "更新菜单" )
    @AutoLog(title = "菜单管理模块修改", businessType = 2) //记录日志使用
    public Result update(@RequestBody SysMenu sysMenu) {
        log.info("update {}",sysMenu);
        sysMenuService.updateById(sysMenu);

        return Result.success();
    }

    //removeById/31
    @DeleteMapping("removeById/{id}")
    @CacheEvict(cacheNames = "sysMenuFindNodes",allEntries=true)
    @Operation( summary = "删除菜单" )
    @AutoLog(title = "菜单管理模块删除", businessType = 3) //记录日志使用
    public Result removeById(@PathVariable("id") String id) {
        log.info("removeById {}",id);
        sysMenuService.removeById(id);

        return Result.success();
    }



}
